package com.ha.garbage.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;


import com.ha.garbage.R;
import com.ha.garbage.context.BaseContext;
import com.ha.garbage.model.News;
import com.ha.garbage.model.Result;
import com.ha.garbage.service.BannerService;
import com.ha.garbage.service.userService;
import com.ha.garbage.utils.UserRetrofit;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.MyHolder>{


private List<News> newsList  = new ArrayList<>();

    public NewsListAdapter(List<News> newsList) {
        this.newsList = newsList;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate((R.layout.news_item), null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {


        //绑定数据
        News news = newsList.get(position);
        //设置数据
        holder.tv_title.setText(news.getTitle());
        holder.tv_summary.setText(news.getSummary());
        holder.imageView.setImageResource(R.drawable.baseline_local_fire_department_24);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("是否跳转网页")
                        .setMessage(news.getNews())
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Uri uri = Uri.parse(news.getNews());
                                try {
                                    Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                    System.out.println("访问uri:"+uri.toString());
                                    intent.setClassName("com.android.chrome","com.google.android.apps.chrome.Main");
                                    v.getContext().startActivity(intent);

                                }catch (Exception e){
                                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                    v.getContext().startActivity(intent);
                                }

                                if(BaseContext.getCurrentId() != null){
                                    Retrofit retrofit = UserRetrofit.retrofit();

                                    userService userService = retrofit.create(userService.class);
                                    Call<Result> resultCall =
                                            userService.setPoint(2, BaseContext.getCurrentId(), BaseContext.getCurrentToken());
                                    resultCall.enqueue(new Callback<Result>() {
                                        @Override
                                        public void onResponse(Call<Result> call, Response<Result> response) {
                                            if(response.isSuccessful()){
                                                Result result = response.body();
                                                if(result.getCode() == 1){
                                                    BaseContext.setCurrentCreditPoint(BaseContext.getCurrentCreditPoint()+30);
                                                }else {
                                                    System.out.println(result.getMsg());
                                                }
                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<Result> call, Throwable t) {

                                            System.out.println("请求路径"+call.request().url().toString());
                                            System.out.println("错误原因"+t.toString());
                                        }
                                    });

                                }




                            }
                        })
                        .show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder{

        TextView tv_title;
        TextView tv_summary;
        ImageView imageView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            //初始化控件
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_summary = itemView.findViewById(R.id.tv_summary);
            imageView = itemView.findViewById(R.id.img_news);

        }
    }
}



